<?php

$name = $_POST['user'];
$password = $_POST['psw1'];
$password2 =$_POST['psw2'];
$code = $_POST['code'];



session_start();
$code1 = $_SESSION['verifyCode'];
if (!($code == $code1)) {
    exit("<script>alert('验证码错误');location.href='register.php'</script>");
} 

if(!($password==$password2)){
    exit("<script>alert('密码重复错误');location.href='register.php'</script>");
}


try{
    $dsn='mysql:host=localhost;dbname=yunbbs;charset=utf8';
    $pdo=new PDO($dsn,'root','123456',array(
        PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
    ));
} catch (PDOException $e){
    echo "数据库连接失败" . $e->getMessage() . "<hr>";
}
$result=$pdo->query("select name from yunbbs_users where name='{$name}'");
$isExist=false;
foreach ($result as $key => $value) {
    $isExist=true;
    break;
}
if($isExist){
    exit("<script>alert('该用户已存在');header.href='register.php'</script>");
}else{
    $pdo->exec("insert into yunbbs_users(name,password)value('$name','$password');");
    header("Location:login.php");
}

$pdo=null;
?>